/**
 * 
 */
package np.com.abhishek.scrambler.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;

import np.com.abhishek.scrambler.utils.ImageChooser;
import np.com.abhishek.scrambler.utils.JFontChooser;
import np.com.abhishek.scrambler.utils.Sound;
import np.com.abhishek.ui.WidgetFactory;

/**
 * @author Abhishek
 * 
 */

/*-------------------:   BASIC WORKINGS	:----------------------------
 Array of buttons (JButton) have been used to represent the tiles. Intitally the user is
 welcomed with a box to enter the dimension of the board (for this frmSizeSelector is used).
 Then according to the dimension the array of buttons are created on the fly and are in solved
 state initially. The "Scramble" button scrambles the the tiles thus similarly the arrayKeeper
 values also gets scrambled and the last location is always the free location. By clicking the
 button that is neighbour of the free button the required button can be shifted.


 /* -----------------:   USED VARIABLES	:----------------------------
 **	Point
 *	freePt   		: stores the location of the free tile
 *	currentPt		: stores the location of the current tile
 *
 ** final int
 *
 *	SCRAMBLE 		: is 0 to represent scrambled condition
 *	SOLVE 			: is 1 to represent solved condition
 *
 ** int
 *
 *	size			: the dimension of board (when user enter 4  a 4x4 board is drawn
 *	tiles			: tiles = no. of buttons (size * size) - 1
 *	arrayKeeper[]	: keeps track of the changes in the array values
 *	winCondition[]	: sotres the winning condition
 *	btnindex		: stores the index of the button clicked
 *	ht				: stores the height of the button
 *	free			: stores the value of the free tile
 *	gap				: the horizontal and vertical gaping between the tiles
 *	winCount		: stores how many tiles have reached the correct posotion
 */

public class MainFrame implements ActionListener {
	public static JFrame frame;

	JMenuBar menuBar;

	static JMenu mnuGame, mnuOption, mnuHelp, subMnuChgCol;

	static JMenuItem mitmChgBrdSz, mitmAbtScrmblr, seeSplash, mitmBtnStyle,
			mitmHelpTopics, mitmExit;

	static JMenuItem mitmBgCol, mitmFgCol, mitmFont;

	static Sound sound;

	final static int STARTUP = 5;

	public static Color fgCol, bgCol;

	public static Font font;

	static Image img1;

	private final int WIDTH = 440;

	private final int HEIGHT = 560;

	private final Dimension screenSize = Toolkit.getDefaultToolkit()
			.getScreenSize();

	WidgetFactory widgetFactory = WidgetFactory.getInstance();

	public MainFrame() {
		frame = new JFrame("JScrambler");

		menuBar = new JMenuBar();

		mnuGame = widgetFactory.createJMenu("Game", 'G', "", this);

		subMnuChgCol = widgetFactory.createJMenu("Change color", 'C', "", this);

		mnuOption = widgetFactory.createJMenu("Option", 'O', "", this);

		mnuHelp = widgetFactory.createJMenu("Help", 'H', "", this);

		mitmChgBrdSz = widgetFactory.createJMenuItem(mnuGame,
				WidgetFactory.PLAIN, "New Game", "", KeyStroke
						.getKeyStroke("F2"), "Change board size", this, true);

		mitmExit = widgetFactory.createJMenuItem(mnuGame, WidgetFactory.PLAIN,
				"Exit", "", null, "Exit", this, false);

		mitmBgCol = widgetFactory.createJMenuItem(subMnuChgCol,
				WidgetFactory.PLAIN, "Change Background", "", KeyStroke
						.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK
								| InputEvent.SHIFT_MASK),
				"Change background color", this, true);

		mitmFgCol = widgetFactory.createJMenuItem(subMnuChgCol,
				WidgetFactory.PLAIN, "Change Foreground", "", KeyStroke
						.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK
								| InputEvent.SHIFT_MASK),
				"Change foreground color", this, false);

		mitmFont = widgetFactory.createJMenuItem(mnuOption,
				WidgetFactory.PLAIN, "Change Font", "", KeyStroke.getKeyStroke(
						KeyEvent.VK_F, InputEvent.CTRL_MASK), "Change font",
				this, true);

		mitmBtnStyle = widgetFactory.createJMenuItem(mnuOption,
				WidgetFactory.PLAIN, "Change Button Style", "", KeyStroke
						.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK),
				"Change button style", this, false);

		mitmHelpTopics = widgetFactory.createJMenuItem(mnuHelp,
				WidgetFactory.PLAIN, "Help Topics", "", KeyStroke
						.getKeyStroke("F1"), "Change button style", this, true);

		seeSplash = widgetFactory.createJMenuItem(mnuHelp, WidgetFactory.PLAIN,
				"See splash", "", null, "See splash screen", this, true);

		mitmAbtScrmblr = widgetFactory.createJMenuItem(mnuHelp,
				WidgetFactory.PLAIN, "About Scrambler", "", null,
				"About scrambler", this, false);

		int x = (screenSize.width - WIDTH) / 2;
		int y = (screenSize.height - HEIGHT) / 2;

		// frame.addKeyListener(this);
		menuBar.add(mnuGame);
		menuBar.add(mnuOption);
		mnuOption.add(subMnuChgCol);
		menuBar.add(mnuHelp);

		frame.setJMenuBar(menuBar);
		frame.setIconImage(WidgetFactory.getInstance().createImage("icon.png"));
		frame.setSize(WIDTH, HEIGHT);
		frame.setLayout(null);
		frame.setLocation(x, y);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
	}

	/*
	 * public void keyPressed(KeyEvent k) { System.out.println("Pressed:" +
	 * k.getKeyChar()); } public void keyReleased(KeyEvent k) {
	 * System.out.println("Released:" + k.getKeyChar()); }
	 * 
	 * public void keyTyped(KeyEvent k) { System.out.println("Typed:" +
	 * k.getKeyChar()); }
	 */

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == mitmChgBrdSz)
			new SizeSelector(frame);

		if (e.getSource() == mitmExit)
			System.exit(0);

		else if (e.getSource() == mitmFgCol) {
			fgCol = JColorChooser.showDialog(null, "Choose Foreground Color",
					Color.white);
			for (int i = 0; i < Initializer.tiles; i++)
				Initializer.btn[i].setForeground(fgCol);
		}

		else if (e.getSource() == mitmBgCol) {
			bgCol = JColorChooser.showDialog(null, "Choose Background Color",
					Color.white);
			for (int i = 0; i < Initializer.tiles; i++)
				Initializer.btn[i].setBackground(bgCol);
		}

		else if (e.getSource() == mitmFont) {
			new JFontChooser();
		}

		else if (e.getSource() == mitmBtnStyle) {
			new ImageChooser();

			File f = ImageChooser.file;
			String imagePath = "";
			try {
				imagePath = f.getAbsolutePath();
			} catch (Exception ex) {
				JOptionPane
						.showMessageDialog(
								frame,
								"Since no file was selected the default \n image file will be loaded.",
								"No Image Selected",
								JOptionPane.INFORMATION_MESSAGE);
				imagePath = "defaultImage.gif";
			}

			int width = Initializer.width;
			int height = Initializer.height;

			img1 = WidgetFactory.getInstance().createImage(imagePath);
			Image img = img1.getScaledInstance(width, height, Image.SCALE_FAST);

			ImageIcon imgIcon = new ImageIcon(img);

			for (int i = 0; i < Initializer.tiles; i++) {
				Initializer.btn[i].setIcon(imgIcon);
				Initializer.btn[i]
						.setHorizontalTextPosition(SwingConstants.CENTER);
			}
		} else if (e.getSource() == mitmAbtScrmblr)
			new AboutScrambler(frame);

		else if (e.getSource() == seeSplash)
			new SplashScreen(frame, 310, 375, "splash.png").show();

	}
}